package Component;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Scene.Renderable;

import Clock.Clock;

public class ComponentPackage extends GameComponent 
{
	public GameComponent[] components;
	
	public String name;
	
	public int GetTypeId()
	{
		return E_PACKAGECOMPONENT;
	}
	
	public void Update(Clock clock)
	{
		if (components != null)
		{
			for(int i = 0; i < components.length; ++i)
			{
				GameComponent comp = components[i];
				comp.Update(clock);			
			}
		};
	}
	
	public void SetEnable(boolean bEnable)
	{
		super.SetEnable(bEnable);
		if (components != null)
		{
			for(int i = 0; i < components.length; ++i)
			{
				GameComponent comp = components[i];
				comp.SetEnable(bEnable);			
			}
		}
	}
	
	
	public void RecursiveGetComponents(ArrayList<GameComponent> retval)
	{
		if (retval == null)
		{
			return;
		}
		if (components != null)
		{

			for(int i = 0; i < components.length; ++i)
			{
				GameComponent comp = components[i];
				retval.add(comp);
				
				if (comp.GetTypeId() ==  E_PACKAGECOMPONENT)
				{
					ComponentPackage comppack = (ComponentPackage)comp;
					comppack.RecursiveGetComponents( retval);					
				}
						
			}
		}
	}
	
	// not including this
	public void QueryComponentsByType(int type, ArrayList<GameComponent> retval, boolean bEnabled)
	{
		if (retval == null)
		{
			retval = new ArrayList<GameComponent>();
		}
		if (components != null)
		{

			for(int i = 0; i < components.length; ++i)
			{
				GameComponent comp = components[i];
				if (comp.GetTypeId() ==  type )
				{
					if (comp.GetEnable() == bEnabled)
					{
						retval.add(comp);
					}
				}
				else if (comp.GetTypeId() ==  E_PACKAGECOMPONENT)
				{
					ComponentPackage comppack = (ComponentPackage)comp;
					comppack.QueryComponentsByType(type, retval, bEnabled);					
				}
						
			}
		}
		
	}
	
	public void QueryComponentsByType(int type, ArrayList<GameComponent> retval)
	{
		if (retval == null)
		{
			retval = new ArrayList<GameComponent>();
		}
		if (components != null)
		{

			for(int i = 0; i < components.length; ++i)
			{
				GameComponent comp = components[i];
				if (comp.GetTypeId() ==  type)
				{
					retval.add(comp);
				}
				else if (comp.GetTypeId() ==  E_PACKAGECOMPONENT)
				{
					ComponentPackage comppack = (ComponentPackage)comp;
					comppack.QueryComponentsByType(type, retval);					
				}
						
			}
		}
	}

	
	public boolean Load(JSONObject json)
	{
		try
		{
			name = json.getString("name");
			
			JSONArray comps = json.getJSONArray("comps");
			
			components = new GameComponent[comps.length()];
			for(int i = 0; i < comps.length(); ++i)
			{
				JSONObject jsonComp = comps.getJSONObject(i);		
				int nType = jsonComp.getInt("type");
				GameComponent comp = GameComponent.createComponetByType(nType, GetHost());
				if (comp != null)
				{
					comp.Load(jsonComp);
					comp.SetParent(this);
					components[i] = comp;					
						
				}
			}
		}
		catch(JSONException e)
		{
			
		}
		return true;
	}
}
